﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using EARTHLib;
using iPhoneConnection;

namespace SimpleReciever
{
    public partial class Form1 : Form
    {

        public IApplicationGE app;
        public bool started = false;
        public iPhone iphone;
        public Bitmap pic;
        public Point p;
        System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
        List<iPhone.Sensors> sensors = new List<iPhone.Sensors>();

        public int count;

        public double alt = -1;

        public Form1()
        {
            InitializeComponent();

            System.Windows.Forms.Form.CheckForIllegalCrossThreadCalls = false;

            iphone = new iPhone(3000);

            iphone.connected += new iPhone.iPhoneConnected(iphone_connected);
            iphone.dataRecieved += new iPhone.DataRecieved(iphone_dataRecieved);
            iphone.disconnected += new iPhone.iPhoneDisconnected(iphone_disconnected);
        }

        void iphone_disconnected()
        {
            Console.WriteLine("iPhone Disconnected");
            MessageBox.Show("Disconnected");
        }

        void iphone_connected()
        {
            Console.WriteLine("iPhone Connected");
            foreach (iPhone.Sensors s in iphone.connectionSettings.enabledSensors)
            {
                textBox1.AppendText(Enum.GetName(typeof(iPhone.Sensors), s) + Environment.NewLine);
            }
        }

        void iphone_dataRecieved()
        {
            double rotation = Math.PI + Math.Atan2(-iphone.AccelerometerReading.Y, -iphone.AccelerometerReading.X);
            double rotation2 = (Math.PI / 2) - Math.Atan2(-iphone.AccelerometerReading.Y, -iphone.AccelerometerReading.Z);

            accTilt.Angle = rotation;
            accTwist.Angle = rotation2;

            compassHeading.Angle = iphone.CompassReading.TrueHeading;
            compassMag.Angle = iphone.CompassReading.MagneticHeading;
            compassX.Angle = iphone.CompassReading.X;
            compassY.Angle = iphone.CompassReading.Y;
            compassZ.Angle = iphone.CompassReading.Z;

            progressBar1.Value = ((int)iphone.DeviceReading.batteryPercent);
            batteryLabel.Text = "Battery State: " + Enum.GetName(typeof(iPhone.BatteryState), iphone.DeviceReading.batteryState);

            gpsData.ResetText();
            gpsData.AppendText("Lattitude: " + iphone.GPSReading.X + Environment.NewLine);
            gpsData.AppendText("Longitude: " + iphone.GPSReading.Y + Environment.NewLine);
            gpsData.AppendText("Altitude: " + iphone.GPSReading.Z + Environment.NewLine);
            gpsData.AppendText("Accuraccy: " + iphone.GPSReading.Accuracy + Environment.NewLine);
            gpsData.AppendText("Speed: " + iphone.GPSReading.Speed);

            gpsHeading.Angle = iphone.GPSReading.Heading;

            if (checkBox1.Checked)
            {
                if (app == null)
                    app = new ApplicationGE();

                app.SetCameraParams(iphone.GPSReading.Y, iphone.GPSReading.X, 1000, AltitudeModeGE.AbsoluteAltitudeGE,
                    0, Math.Min(90, Math.Max(0, iphone.CompassReading.Y * -2)), iphone.CompassReading.TrueHeading, 2);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!started)
            {
                iphone.BeginListening();
                button1.Text = "Stop";
            }
            else
            {
                iphone.StopListening();
                button1.Text = "Start";
            }
            started = !started;
        }
    }
}
